package Robot;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import com.example.storage.R;
import com.iflytek.sparkchain.core.LLM;
import com.iflytek.sparkchain.core.LLMConfig;
import com.iflytek.sparkchain.core.LLMOutput;
import com.iflytek.sparkchain.core.SparkChain;
import com.iflytek.sparkchain.core.SparkChainConfig;
import org.json.JSONArray;
import org.json.JSONObject;

public class robot extends AppCompatActivity {

    private static final String TAG = "robot";
    private String appId = "d398330f";
    private String apiKey = "4d7d60a15659f88b9a070efd7bb916c1";
    private String apiSecret = "YjAxZWNkYWIzMjQ1Y2Q3MDcwYmM3NzUw";
    private String results = "";
    private boolean isFinish = false;

    static {
        System.loadLibrary("SparkChain");  // 确保加载本地库
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot);

        logDeviceArchitecture();

        initializeSDK(); // 初始化SDK
        configureLLM(); // 配置大模型参数
        performSyncCall("上海有什么景点？"); // 进行同步调用
    }

    private void initializeSDK() {
        SparkChainConfig config = SparkChainConfig.builder()
                .appID(appId)
                .apiKey(apiKey)
                .apiSecret(apiSecret);

        int ret = SparkChain.getInst().init(getApplicationContext(), config);
        Log.d(TAG, "SDK init: " + ret);
    }

    private void configureLLM() {
        LLMConfig llmConfig = LLMConfig.builder()
                .domain("generalv2")
                .url("wss://spark-api.xf-yun.com/v2.1/chat");
        LLM llm = new LLM(llmConfig);
    }

    private void performSyncCall(String question) {
        LLM llm = new LLM(getLLMConfig());
        LLMOutput syncOutput = llm.run(question);
        handleLLMOutput(syncOutput);

        // 第二轮交互，带历史上下文
        String question2 = "那帮我安排一份旅游计划吧。";
        try {
            JSONArray array = new JSONArray();
            JSONObject item1 = new JSONObject();
            item1.put("role", "user");
            item1.put("content", question);
            JSONObject item2 = new JSONObject();
            item2.put("role", "assistant");
            item2.put("content", results);
            JSONObject item3 = new JSONObject();
            item3.put("role", "user");
            item3.put("content", question2);
            array.put(item1).put(item2).put(item3);

            syncOutput = llm.run(array.toString());
            handleLLMOutput(syncOutput);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private LLMConfig getLLMConfig() {
        return LLMConfig.builder()
                .domain("generalv2")
                .url("wss://spark-api.xf-yun.com/v2.1/chat");
    }

    private void handleLLMOutput(LLMOutput output) {
        if (output.getErrCode() == 0) {
            Log.i(TAG, "Sync call: " + output.getRole() + ": " + output.getContent());
            results = output.getContent();
        } else {
            Log.e(TAG, "Sync call: errCode " + output.getErrCode() + " errMsg: " + output.getErrMsg());
        }
    }

    public void logDeviceArchitecture() {
        String TAG = "DeviceInfo";
        String[] supportedAbis;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            supportedAbis = Build.SUPPORTED_ABIS;
        } else {
            supportedAbis = new String[]{Build.CPU_ABI, Build.CPU_ABI2};
        }

        for (String abi : supportedAbis) {
            Log.d(TAG, "Supported ABI: " + abi);
        }
    }
}
